import Vue from "vue";
import {Message} from 'element-ui';

const path = require('path')
const files = require.context('./business', true, /\.js/)
const modules = {}
files.keys().forEach(key => {
  // 提取出用 ‘/' 隔开的path的最后一部分,path.basename(p, [ext])。 p要处理的path,ext要过滤的字符
  const name = path.basename(key, '.js')
  if(name === 'index'){ return; }
  modules[name] = files(key).default || files(key)
})

// api
function Api(name,param,options){
  let option = options || {};
  let request = _.get(modules,name);
  if(!request){
    console.warn(`API : ${name} not found!`)
    return;
  }
  //
  return new Promise((res,rej)=>{
    option.loading = true;
    request(param).then((response)=>{
      let result = response;
      res(result);
    },(e)=>{
      rej(e);
    }).finally(()=>{
      option.loading = false;
    })
  })
}

//
export default Api;
